package cn.haona.ftp;

import java.io.File;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;

/**
 * 
 */
public class FtpFile {
    private String host;
    private String user;
    private String password;
    private String ftpPath;
    private final Logger logger = Logger.getLogger(getClass());

    /**
     * 
     */
    public FtpFile() {

    }

    /**
     * 
     */
    public FtpFile(String host, String username, String pwd) {
        this.host = host;
        this.user = username;
        this.password = pwd;

    }

    /**
     * TODO 获取FTP文件
     * 
     * @param remotehost
     *                主机IP地址
     * @param username
     *                用户名称
     * @param pwd
     *                用户密码
     * @param filepath
     *                ftp文件路径
     * @param downpath
     *                下载文件保存路径
     * @param action
     *                int 下载后是否删除 0不删除1删除
     * @return filename 返回文件名称
     */

    public String getFile(String remotehost, String username, String pwd,
        String filepath, String filename, String downpath, int action) {
        try {
            /**
             * 服务器建立连接
             */
            FTPClient ftp = new FTPClient();
            ftp.setRemoteHost(remotehost);
            ftp.setControlEncoding("gbk");
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);
            ftp.connect();

            /**
             * 登陆到Ftp服务器
             */
            ftp.login(username, pwd);
            ftp.setConnectMode(FTPConnectMode.PASV);
            ftp.setType(FTPTransferType.BINARY);

            /**
             * 下载文件
             */
            ftp.chdir(filepath);
            ftp.get(downpath + File.separator + filename, filename);

            /**
             * 删除文件
             */
            if (action == 1) {
                ftp.delete(filename);
                this.logger.info("delete ok");
            }

            ftp.quit();
        } catch (Exception e) {
            this.logger.error(e, e);
        }
        return filename;
    }

    /**
     * 
     */
    public String getFile(boolean pasv, boolean ascii, String remotehost,
        String username, String pwd, String filepath, String filename,
        String downpath, int action) {
        try {
            /**
             * 服务器建立连接
             */
            FTPClient ftp = new FTPClient();
            ftp.setRemoteHost(remotehost);
            ftp.setControlEncoding("gbk");
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);
            ftp.connect();

            /**
             * 登陆到Ftp服务器
             */
            ftp.login(username, pwd);
            if (pasv) {
                ftp.setConnectMode(FTPConnectMode.PASV);
            } else {
                ftp.setConnectMode(FTPConnectMode.ACTIVE);
            }
            if (ascii) {
                ftp.setType(FTPTransferType.ASCII);
            } else {
                ftp.setType(FTPTransferType.BINARY);
            }

            /**
             * 下载文件
             */
            ftp.chdir(filepath);
            ftp.get(downpath + File.separator + filename, filename);

            /**
             * 删除文件
             */
            if (action == 1) {
                ftp.delete(filename);
                this.logger.info("delete ok");
            }

            ftp.quit();
        } catch (Exception e) {
            this.logger.error(e, e);
        }
        return filename;
    }

    /**
     * 
     */
    public int getFileFromFtp(String remotehost, String username, String pwd,
        String filepath, String filename, String downpath, int action) {
        int flag = 0;
        try {
            /**
             * 服务器建立连接
             */
            FTPClient ftp = new FTPClient();
            ftp.setRemoteHost(remotehost);
            ftp.setControlEncoding("gbk");
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);
            ftp.connect();

            /**
             * 登陆到Ftp服务器
             */
            ftp.login(username, pwd);
            ftp.setConnectMode(FTPConnectMode.PASV);
            ftp.setType(FTPTransferType.BINARY);

            /**
             * 下载文件
             */
            ftp.chdir(filepath);
            ftp.get(downpath + File.separator + filename, filename);
            flag = 1;
            /**
             * 删除文件
             */
            if (action == 1) {
                ftp.delete(filename);
                this.logger.info("delete ok");
            }

            ftp.quit();
        } catch (Exception e) {
            this.logger.error("Failed to open file");
        }
        return flag;
    }

    /**
     * 
     */
    private boolean putFileToFtp(String localfile, String uploadFileName) {
        boolean retftp = false;
        try {
            /**
             * 服务器建立连接
             */
            FTPClient ftp = new FTPClient();
            ftp.setRemoteHost(host);
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);
            ftp.connect();

            /**
             * 登陆到Ftp服务器
             */
            ftp.login(user, password);
            ftp.setConnectMode(FTPConnectMode.PASV);
            ftp.setType(FTPTransferType.BINARY);

            /**
             * 上传文件
             */
            try {
                ftp.chdir(this.ftpPath);
            } catch (Exception ex) {
                ftp.mkdir(this.ftpPath);
                ftp.chdir(this.ftpPath);
            }

            if ("".equals(uploadFileName)) {
                File file = new File(localfile);
                uploadFileName = file.getName();
            }
            ftp.put(localfile, uploadFileName);
            ftp.quit();
            retftp = true;
        } catch (Exception e) {
            this.logger.error(e, e);
        }
        return retftp;
    }

    /**
     * 
     */
    public boolean putFile(String remotehost, String username, String pwd,
        String ftppath, String filename, String localfile) {
        this.ftpPath = ftppath;
        this.host = remotehost;
        this.user = username;
        this.password = pwd;
        return putFileToFtp(localfile, filename);
    }

    /**
     * 
     */
    public boolean putFile(String localfile, String ftppath, String filename) {
        this.ftpPath = ftppath;
        return putFileToFtp(localfile, filename);
    }

    /**
     * 
     */
    public boolean putFile(String localfile, String ftppath) {
        this.ftpPath = ftppath;
        return putFileToFtp(localfile, "");
    }

    /**
     * 
     */
    public boolean putFile(String localfile) {
        return putFileToFtp(localfile, "");
    }

    /**
     * 
     */
    public boolean initFtpConfig() {
        return initFtpConfig("ftp.conf");
    }

    /**
     * 
     */
    public boolean initFtpConfig(String confFile) {
        ResourceBundle CONF = ResourceBundle.getBundle(confFile);
        this.host = CONF.getString("host");
        this.user = CONF.getString("user");
        this.password = CONF.getString("password");
        this.ftpPath = CONF.getString("ftppath");
        return true;
    }

    /**
     * 
     */
    public String getHost() {
        return host;
    }

    /**
     * 
     */
    public String getPassword() {
        return password;
    }

    /**
     * 
     */
    public String getUser() {
        return user;
    }

    /**
     * 
     */
    public String getFtpPath() {
        return ftpPath;
    }

    /**
     * 
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * 
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * 
     * @param ftpPath
     */
    public void setFtpPath(String ftpPath) {
        this.ftpPath = ftpPath;
    }

    /**
     * public static void main(String[] args) { FtpFile ftp = new
     * FtpFile("59.151.33.197", "ftpupld", "hjkw4tr8(^T*jklfds");
     * ftp.putFile("/data/out.mp3", "./bjquku/dtest"); }
     */
}
